package org.zxp.datastructure.tree;

import org.junit.Test;
import org.zxp.datastructure.tree.LinkedTree;

/**
 * @program: effectiveJava
 * @description: 二叉树遍历
 * @author: X-Pacific zhang
 * @create: 2019-08-02 15:28
 **/
public class TreeErgodic {
    LinkedTree node = null;

    private void initLinkedTree(){
        LinkedTree t1 = new LinkedTree("A");
        LinkedTree t2 = new LinkedTree("B");
        LinkedTree t3 = new LinkedTree("C");
        LinkedTree t4 = new LinkedTree("D");
        LinkedTree t5 = new LinkedTree("E");
        LinkedTree t6 = new LinkedTree("F");
        LinkedTree t7 = new LinkedTree("G");
        t1.setLeft(t2);
        t1.setRight(t3);
        t2.setLeft(t4);
        t2.setRight(t5);
        t3.setLeft(t6);
        t3.setRight(t7);

        node = t1;
    }

    /**
     * 测试遍历二叉树
     */
    @Test
    public void testErgodic(){
        initLinkedTree();
        print3(node);
    }

    /**
     * 前序遍历  先打印节点 在打印left  在打印right
     * @param t
     */
    static void print1(LinkedTree t){
        if(t == null)return;
        System.out.println(t.getValue());
        print1(t.getLeft());
        print1(t.getRight());
    }

    /**
     * 中序遍历 left 本 right
     * @param t
     */
    static void print2(LinkedTree t){
        if(t == null)return;
        print2(t.getLeft());
        System.out.println(t.getValue());
        print2(t.getRight());
    }

    /**
     * 后序遍历 left right 本
     * @param t
     */
    static void print3(LinkedTree t){
        if(t == null)return;
        print3(t.getLeft());
        print3(t.getRight());
        System.out.println(t.getValue());
    }
}
